<?php
/**
 * 《财务》实用类
 *
 * @author 孙晓晔
 * @version $Id: InfoUtil.class.php 636 2009-11-25 05:49:36Z sunxy $
 */
require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Finance extends FinanceUtil {
	
	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
	}
	
	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['财务管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function exchange($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE type = '1' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_tec);		
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['ok'] = true; 

			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', json_encode($data));
		$controller->display($request, 'finance/roe/update');
		return true;
	}
	
	function exchangePost($controller, $request) {		
		$db = newdb($this->dsn_w);		
		while (list($k, $v) = each($request['data'])) {
			$code = trim($v['code']);
			$name = trim($v['name']);
			$data = trim($v['data']);
			$content = addslashes(trim($v['content']));
			
			$key = "exchange.ajax_" . $code;
			clean($key);
						
			if (array_key_exists("createtime", $v)) {
				if (!$this->_ok($v['ok'])) {
					$fmt = "DELETE FROM %s WHERE id = '%s'";
					$sql = sprintf($fmt, Constant::tbl_tec, $v['id']);
				} else {
					if ($v['status'] > 1) {
						continue;
					}
					
					$fmt = "UPDATE %s SET code = '%s', name = '%s', data = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
					$sql = sprintf($fmt, Constant::tbl_tec, $code, $name, $data, $content, $v['id']);
				}			
			} else {
				if (!$this->_ok($v['ok'])) {
					continue;
				}
				
				$fmt = "INSERT INTO %s(code, name, data, content, createtime, modifytime)";
				$fmt .= " VALUES('%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, Constant::tbl_tec, $code, $name, $data, $content);			
			}
			
			debug($sql);
			$db->query($sql);
		}
		
		$fmt = "INSERT INTO %s(code, name, data, content, createtime) VALUES('%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, Constant::tbl_currency_exchange, $code, $name, $data, $content);
		$db->query($sql);	
	}	
	
	function roe($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE type = '1' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_tec, $id);		
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['ok'] = true; 

			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', json_encode($data));
		$controller->display($request, 'finance/roe/list');
		return true;
	}
}